import {Component, OnInit, Input, OnChanges, SimpleChanges} from '@angular/core';
import {NzIconService} from 'ng-zorro-antd';
import {ConfigService} from '@kylin/config';

@Component({
    selector: 'kylin-icon',
    templateUrl: './icon.component.html',
    styleUrls: ['./icon.component.css']
})
export class IconComponent implements OnInit, OnChanges {

    iconfont = ''; // 使用iconfont项目上的icon时候,只传这个
    type = ''; // 使用ng-zorro上icon时候,只传入这个
    @Input() theme = 'outline'; // 使用ng-zorro上icon时候可选项
    @Input() icon = '';

    constructor(private nzIconService: NzIconService,
                private configService: ConfigService) {
        this.nzIconService.fetchFromIconfont(this.configService.fetchFromIconfontConfig());
    }

    ngOnInit(): void {
    }

    ngOnChanges(changes: SimpleChanges): void {
        if (changes.icon) {
            const currentValue = changes.icon.currentValue;
            if (currentValue) {
                if (currentValue.indexOf('icon-') === 0) {
                    this.iconfont = currentValue.substring(5);
                    this.type = '';
                } else {
                    this.iconfont = '';
                    this.type = currentValue;
                }
            }
        }
    }

}
